﻿
using IntelligentGateway.Data;
using IntelligentGateway.Model;
using IntelligentGateway.Tools;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Reflection;
using System.Text;

namespace IntelligentGateway.Services
{
    public class CommServices
    {
        public static async Task<WebSocketResponse> ReflectInvoke(WebSocketRequest Request)
        {
            string strRequest = JsonConvert.SerializeObject(Request);
#if DEBUG
            Log.Debug($"ReflectInvoke:" + strRequest);
#endif
            WebSocketResponse response = new WebSocketResponse();
            try
            {
                Type type = Type.GetType("IntelligentGateway.Services.IntelligentGatewayServices");
                var method = type.GetMethod(Request.method, BindingFlags.Static | BindingFlags.Public);
                Task<WebSocketResponse> t = (Task<WebSocketResponse>)method.Invoke(type, new object[] { strRequest });
                response = await t;
            }
            catch (Exception ex)
            {
#if DEBUG
                Log.Debug("ReflectInvoke:" + ex.ToString());
#endif
            }
            if (response != null)
            {
                response.method = Request.method;
                response.req_id = Request.req_id;
            }
            return response;
        }
        public static int GetNextNum(int projectId, string typeId)
        {
            lock (GlobalVariables.NextNumberLock)
            {
                return CommDA.GenNextNum(projectId, typeId);
            }
        }
        public static bool GetDeviceList()
        {
            DeviceInfoDA dA = new DeviceInfoDA();
            //MasterDeviceInfo masterDeviceInfo = dA.GetMasterDeviceInfo();
            List<MasterDeviceInfo> masterDeviceInfo = dA.GetAllMasterDeviceInfo();
            if (masterDeviceInfo != null && masterDeviceInfo.Count > 0)
            {
                foreach (var item in masterDeviceInfo)
                {
                    GlobalVariables.DeviceList.Add(new DeviceMessage { DeviceIP = item.DeviceIP, NO = item.NO, ByteNO = BitConverter.GetBytes(item.NO),SignType=item.SignType });
                    int pid = item.ID;
                    List<SubDeviceInfo> lst = dA.GetSubDeviceInfo(pid);
                    if (lst != null && lst.Count > 0)
                    {
                        foreach (var t in lst)
                        {
                            GlobalVariables.DeviceList.Add(new DeviceMessage { DeviceIP = t.DeviceIP, NO = t.NO, ByteNO = BitConverter.GetBytes(t.NO), SignType = item.SignType });
                        }
                    }
                }
               
                return true;
            }
            else return false;
        }

        public static bool SignalPointList()
        {
            SignalPointDA dA = new SignalPointDA();
            List<SignalPoint> list = new List<SignalPoint>();
            try
            {
                list = dA.GetAllPointListData("内呼");
                if (list != null)
                {
                    GlobalVariables.SignalPointList = list;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return true;
            }
        }
    }
}
